package com.xingchi.framework.common.utils;

import com.xingchi.framework.common.model.RegexConstant;

import java.util.Map;

/**
 * url相关工具类
 *
 * @author xingchi
 * @date 2022/10/22 13:52
 * @modified xingchi
 */
public class UrlUtils {

    private static final String COMMA = ",";
    private static final String JOINT_MARK = "&";
    private static final String QUESTION_MARK = "?";
    private static final String EQUAL_MARK = "=";

    /**
     * 设置Url的参数
     *
     * @param domain        url
     * @param map           需要拼接的参数
     * @return              拼接好的url
     */
    public static String mapToUrl(String domain, Map<String, Object> map) {
        if (!domain.matches(RegexConstant.REGEX_URL)) {
            throw new IllegalArgumentException(String.format("'%s' it's not a url ", domain));
        }
        StringBuilder sb = new StringBuilder(domain + QUESTION_MARK);
        map.forEach((key, value) -> {
            sb.append(key).append(EQUAL_MARK).append(value).append(JOINT_MARK);
        });
        return sb.deleteCharAt(sb.lastIndexOf(JOINT_MARK)).toString();
    }
}
